﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using  DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

namespace DevEx
{
    public partial class ucDonVi : DevExpress.XtraEditors.XtraUserControl
    {
        CDonVi m_DonVi = new CDonVi();
        private tblDonVi _donvi ;
       
        public ucDonVi()
        {
            InitializeComponent();
        }

        private void ucDonVi_Load(object sender, EventArgs e)
        {
            KhoiTaotreelistDonVi();
        }

        public void KhoiTaotreelistDonVi()
        {
            tlDonVi.LookAndFeel.UseDefaultLookAndFeel = false;
            tlDonVi.LookAndFeel.SkinName = "iMaginary";
            tlDonVi.OptionsBehavior.Editable = false;
            //tlDonVi.OptionsView.ShowColumns = false;
            tlDonVi.OptionsView.ShowIndicator = false;
            tlDonVi.OptionsView.ShowHorzLines = false;
            tlDonVi.OptionsView.ShowVertLines = false;
            tlDonVi.SelectImageList = imageCollection1;
                        
            tlDonVi.DataSource = m_DonVi.SelectAllRecordByCuoi(false);
            tlDonVi.KeyFieldName = "ID_DonVi";
            tlDonVi.ParentFieldName = "ID_Parent";
            tlDonVi.Columns.ColumnByFieldName("DiaChi").Visible = false;
            tlDonVi.Columns.ColumnByFieldName("Nhom").Visible = false;
            tlDonVi.Columns.ColumnByFieldName("Cuoi").Visible = false;
            tlDonVi.Columns.ColumnByFieldName("DonVi").Caption = "Đơn vị";
        }

        public void KhoiTaotreelistCTDonVi(int parentID)
        {
            tlCTDonVi.LookAndFeel.UseDefaultLookAndFeel = false;
            tlCTDonVi.LookAndFeel.SkinName = "iMaginary";
            tlCTDonVi.OptionsBehavior.Editable = false;
            //tlDonVi.OptionsView.ShowColumns = false;
            tlCTDonVi.OptionsView.ShowIndicator = false;
            //tlCTDonVi.OptionsView.ShowHorzLines = false;
            //tlCTDonVi.OptionsView.ShowVertLines = false;
            tlCTDonVi.SelectImageList = imageCollection2;
            tlCTDonVi.OptionsView.ShowRoot = false;
            tlCTDonVi.OptionsBehavior.KeepSelectedOnClick = false;

            tlCTDonVi.DataSource = m_DonVi.SelectAllRecordByParentID(parentID);
            tlCTDonVi.KeyFieldName = "ID_DonVi";
            tlCTDonVi.ParentFieldName = "ID_Parent";
            tlCTDonVi.Columns.ColumnByFieldName("Nhom").Visible = false;
            tlCTDonVi.Columns.ColumnByFieldName("Cuoi").Visible = false;
            tlCTDonVi.Columns.ColumnByFieldName("DonVi").Caption = "Đơn vị";
            tlCTDonVi.Columns.ColumnByFieldName("DiaChi").Caption = "Địa chỉ";
        }

        private void tlDonVi_CustomDrawNodeCell(object sender, CustomDrawNodeCellEventArgs e)
        {
            TreeList tl = sender as TreeList;
            if (e.Node == tl.FocusedNode)
            {
                e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
                Rectangle rect = new Rectangle(e.EditViewInfo.ContentRect.Left, e.EditViewInfo.ContentRect.Top,
                    Convert.ToInt32(e.Graphics.MeasureString(e.CellText, tlDonVi.Font).Width + 1),
                    Convert.ToInt32(e.Graphics.MeasureString(e.CellText, tlDonVi.Font).Height + 1));
                e.Graphics.FillRectangle(SystemBrushes.Highlight, rect);
                e.Graphics.DrawString(e.CellText, tlDonVi.Font, SystemBrushes.HighlightText, rect);
                e.Handled = true;
            }
        }

        private void tlDonVi_FocusedNodeChanged(object sender, FocusedNodeChangedEventArgs e)
        {
            try
            {
                int parentID = Convert.ToInt32(e.Node.GetValue("ID_DonVi"));
                KhoiTaotreelistCTDonVi(parentID);
                e.Node.SelectImageIndex = 2;
            }
            catch { 
            }          
            
        }

        private void thêmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int parentID = Convert.ToInt32(tlDonVi.FocusedNode.GetValue("ID_DonVi"));
            tblDonVi donvi = new tblDonVi();
            donvi.DonVi = "New don vi";
            donvi.DiaChi = "";
            donvi.Nhom = "";
            donvi.Cuoi = true;
            donvi.ID_Parent = parentID;
            m_DonVi.Insert(donvi);
            KhoiTaotreelistCTDonVi(parentID);
            // tìm vị trí node và editor
            //tlCTDonVi.FocusedNode = 
            for (int i = 0; i <= tlCTDonVi.Nodes.Count - 1; i++)
            {
                if (donvi.DonVi == tlCTDonVi.Nodes[i]["DonVi"].ToString())
                {
                    tlCTDonVi.OptionsBehavior.Editable = true;
                    tlCTDonVi.FocusedNode = tlCTDonVi.Nodes[i];
                    tlCTDonVi.ShowEditor();                
                }
            }
        }

        private void sửaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tlCTDonVi.OptionsBehavior.Editable = true;
            tlCTDonVi.ShowEditor();            
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int parentID = Convert.ToInt32(tlDonVi.FocusedNode.GetValue("ID_DonVi"));
            int ID_DonVi = Convert.ToInt32(tlCTDonVi.FocusedNode.GetValue("ID_DonVi"));
            m_DonVi.Delete(ID_DonVi);
            KhoiTaotreelistCTDonVi(parentID);
        }

        private void tlCTDonVi_FocusedNodeChanged(object sender, FocusedNodeChangedEventArgs e)
        {
            try
            {
               
                    //if (tlCTDonVi.OptionsBehavior.Editable)
                    //{
                    //    //update DonVi
                    //tblDonVi donvi = new tblDonVi();
                    //_donvi.ID_DonVi = Convert.ToInt32(e.OldNode.GetValue("ID_DonVi"));
                    //_donvi.DonVi = e.OldNode.GetValue("DonVi").ToString();
                    //_donvi.DiaChi = e.OldNode.GetValue("DiaChi").ToString();
                    //_donvi.Cuoi = Convert.ToBoolean(e.OldNode.GetValue("Cuoi"));
                    //_donvi.ID_Parent = Convert.ToInt32(e.OldNode.GetValue("ID_Parent"));
                    //    m_DonVi.Update(donvi);

                    //    //KhoiTao lai tlDonVi
                    //    KhoiTaotreelistDonVi();
                    //    tlDonVi.FocusedNode = e.Node;
                    //    tlDonVi.ExpandAll();
                    //}                    

                    //tlCTDonVi.OptionsBehavior.Editable = false;
              
            }
            catch {  }           
            
        }

        private void tlCTDonVi_HiddenEditor(object sender, EventArgs e)
        {
            //update DonVi
            //m_DonVi.Update(_donvi);
            

            //tlCTDonVi.OptionsBehavior.Editable = false;
        }

        private void tlCTDonVi_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            _donvi = new tblDonVi();
            _donvi.ID_DonVi = Convert.ToInt32(e.Node.GetValue("ID_DonVi"));
            _donvi.DonVi = e.Node.GetValue("DonVi").ToString();
            _donvi.DiaChi = e.Node.GetValue("DiaChi").ToString();
            _donvi.Cuoi = Convert.ToBoolean(e.Node.GetValue("Cuoi"));
            _donvi.ID_Parent = Convert.ToInt32(e.Node.GetValue("ID_Parent"));
            m_DonVi.Update(_donvi);
            tlCTDonVi.OptionsBehavior.Editable = false;
        }

        private void thêmNhómToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int parentID = Convert.ToInt32(tlDonVi.FocusedNode.GetValue("ID_DonVi"));
            tblDonVi donvi = new tblDonVi();
            donvi.DonVi = "Nhóm Đơn vị mới";
            donvi.DiaChi = "";
            donvi.Nhom = "";
            donvi.Cuoi = false;
            donvi.ID_Parent = parentID;
            m_DonVi.Insert(donvi);
            KhoiTaotreelistCTDonVi(parentID);
            // tìm vị trí node và editor
            //tlCTDonVi.FocusedNode = 
            for (int i = 0; i <= tlCTDonVi.Nodes.Count - 1; i++)
            {
                if (donvi.DonVi == tlCTDonVi.Nodes[i]["DonVi"].ToString())
                {
                    tlCTDonVi.OptionsBehavior.Editable = true;
                    tlCTDonVi.FocusedNode = tlCTDonVi.Nodes[i];
                    tlCTDonVi.ShowEditor();
                }
            }
        }

        private void tlCTDonVi_GetSelectImage(object sender, GetSelectImageEventArgs e)
        {
            Boolean s = Convert.ToBoolean(e.Node.GetValue("Cuoi"));
            if (s == false) e.NodeImageIndex = 0;
            else if (s == true) e.NodeImageIndex = 1;            
        }

        private void tlDonVi_GetSelectImage(object sender, GetSelectImageEventArgs e)
        {
            if (e.Node.ParentNode == null) e.NodeImageIndex = 0;
            else e.NodeImageIndex = (e.FocusedNode) ? 2 : 1;
        }

        private void tlCTDonVi_DoubleClick(object sender, EventArgs e)
        {
            tlCTDonVi.OptionsBehavior.Editable = true;
            tlCTDonVi.ShowEditor();
        }               
       
    }
}
